package controler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Document;
import model.beans.MinutaBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.upload.FormFile;

import controler.forms.DocumentForm;

public class DocumentAction extends MappingDispatchAction {
	
	private final String DEFAULT = "default";
	private final String SUCCESS = "save_success";
	private final String FAIL = "save_fail";
	private final String DRAFT = "bean_minuta";
	private final String DOCUMENTID = "document_id";
	private final int ZERO = 0;
	
	
	/**
	 * Listado de Documentos por minuta
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		MinutaBean minuta_bean = (MinutaBean) session.getAttribute(DRAFT);
		
		minuta_bean.setArrL_documento(business.Documento.list(minuta_bean.getInt_tk()));
		
		session.setAttribute(DRAFT, minuta_bean);
		
		return mapping.findForward(DEFAULT);
	}
	
	
	/**
	 * 
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String str_documento = request.getParameter(DOCUMENTID);
		int int_documento = str_documento!=null?Integer.parseInt(str_documento):ZERO;
		business.Documento.delete(int_documento);
		session.setAttribute("req_msg", getResources(request).getMessage("Document.SuccessDelete"));
		return mapping.findForward(DEFAULT);
	} 
	
	public ActionForward save(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		MinutaBean minuta_bean = (MinutaBean) session.getAttribute(DRAFT);
		
		String str_forward = SUCCESS;
		String str_msg = getResources(request).getMessage("Document.SuccessAdd");
		DocumentForm myForm = (DocumentForm)form;
		
		FormFile myFile     = myForm.getDocument();
		byte[] fileData    = myFile.getFileData();
		String str_nombre   = myFile.getFileName();
		
		try{
			Map map = new HashMap();
			map.put("int_minuta", new Integer( minuta_bean.getInt_tk() ));
			map.put("str_descripcion", myForm.getDescription());
			map.put("byte_documento", fileData);
			map.put("nombre_documento", str_nombre);
			Document.add( map );
		}catch( Exception ex ){
			str_forward = FAIL;
			str_msg = getResources(request).getMessage("Document.FailAdd");
		}
    
    	session.setAttribute("req_msg", str_msg);
    	myForm.reset(mapping, request);
		return mapping.findForward(str_forward);
	}
	
	public ActionForward show(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		String str_forward = DEFAULT;
		int int_documento = Integer.parseInt(request.getParameter(DOCUMENTID));
		String str_path = request.getRealPath("/tmp");
		request.setAttribute("req_doc", "tmp/"+Document.getDocument(int_documento, str_path));
		return mapping.findForward(str_forward);
	}
}
